package com.game.eggscatcher.GameHelpers;

import javax.annotation.processing.Processor;

import com.badlogic.gdx.InputProcessor;
import com.game.eggscatcher.GameObjects.Ro;
import com.game.eggscatcher.GameWorld.GameWorld;

public class InputHandler implements InputProcessor{
	GameWorld gameWorld;
	Ro ro;
	private float scaleFactorX;
	private float scaleFactorY;
	public InputHandler(GameWorld world, float scaleFactorX, float scaleFactorY) {
		gameWorld = world;
		this.scaleFactorX = scaleFactorX;
		this.scaleFactorY = scaleFactorY;
	}
	@Override
	public boolean keyDown(int keycode) {
		System.out.println("KeyDown "+keycode);
		switch (keycode) {
		case 21: //left
			gameWorld.getRo().moveLeft();
			break;
		case 22: //right
			gameWorld.getRo().moveRight();
			break;
		case 35: //g
			gameWorld.addGa(gameWorld.getLevel());
			break;
		case 42: //n
			gameWorld.addNgan(gameWorld.getLevel());
			break;
		case 46: //r
			AssetLoader.resetHighScore();
			break;
		case 50: //v
			gameWorld.addVit(gameWorld.getLevel());
			break;
		case 66: //enter
			if (gameWorld.isReady()) gameWorld.startGame();
			if (gameWorld.isGameOver()) gameWorld.restartGame();
			if (gameWorld.isLevelUp()) gameWorld.restartGame();
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		System.out.println("KeyUp");
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		System.out.println("KeyTyped");
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		System.out.println("TouchDown");
		screenX = scaleX(screenX);
		if (gameWorld.isReady()) gameWorld.startGame();
		if (gameWorld.isGameOver()) gameWorld.restartGame();
		if (gameWorld.isLevelUp()) gameWorld.restartGame();
		if (gameWorld.isRunning()) gameWorld.getRo().update(screenX);
		
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		System.out.println("TouchUp");
		
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		System.out.println("TouchDragged: "+" "+screenX+" / "+pointer);
		screenX = scaleX(screenX);
		gameWorld.getRo().update(screenX);
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		System.out.println("MouseMoved");
		screenX = scaleX(screenX);
		gameWorld.getRo().update(screenX);
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		System.out.println("Scrolled");
		return false;
	}
	private int scaleX(int screenX) {
        return (int) (screenX / scaleFactorX);
    }

    private int scaleY(int screenY) {
        return (int) (screenY / scaleFactorY);
    }
}
